package com.xy.shop.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xy.shop.common.dtgrid.model.Pager;
import com.xy.shop.web.entity.OperationLog;
import com.xy.shop.web.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@RequestMapping("/monitor/")
@Controller
public class MonitorController extends BaseController{

    @Autowired
    private OperationLogService operationLogService;



    /**
     * @Description druid监控页面
     * @Author jxp
     **/
    @GetMapping("druidUi")
    public String druidUi() {
        return "web/druid";
    }

    /**
     * @Description 操作日志
     * @Author jxp
     **/
    @GetMapping("operationLogUi")
    public String operationLogUi() {
        return "web/operation_log_list";
    }

    @ResponseBody
    @PostMapping(value = "operationLog")
    public Map<String, Object> goldBalanceList(String gridPager) {
        Pager pager = JSON.parseObject(gridPager, Pager.class);
        Map<String, Object> parameters = pager.getParameters();
        this.formartTime(parameters);
        // 设置分页
        Page<Object> page = PageHelper.startPage(pager.getNowPage(), pager.getPageSize());
        List<OperationLog> list = operationLogService.selectList(parameters);
        return getReturnPage(pager, page, list);
    }


    private void formartTime(Map<String,Object> map){
        if(map.containsKey("stime")){
            map.put("stime",map.get("stime").toString()+" 00:00:00");
        }
        if(map.containsKey("etime")){
            map.put("etime",map.get("etime").toString()+" 23:59:59");
        }
    }
}